package pro.softzhang.algo.lc700;

import pro.softzhang.algo.flag.knowledge.KPrefixSum;

import java.util.Arrays;

public class LC724_FindPivotIndex {
    public static void main(String[] args) {
        int[] nums = new int[]{1,7,3,6,5,6};
        System.out.println(new Solution().pivotIndex(nums));
    }

    @KPrefixSum
    static class Solution {
        public int pivotIndex(int[] nums) {
            // int sum = Arrays.stream(nums).sum(), prefixSum = 0;
            int sum = 0, prefixSum = 0;
            for(int num : nums) {
                sum += num;
            }
            for (int i = 0; i < nums.length; i++) {
                prefixSum += nums[i];
                if(prefixSum - nums[i] == sum - prefixSum) {
                    return i;
                }
            }
            return -1;
        }
    }
}
